// 引入mongoose第三方模块
const mongoose = require('mongoose');
const User=require("../app.js")
const Book=require("./book.js")

const articleSchema = new mongoose.Schema({
    //作者id
    author_id:{
        type:mongoose.Schema.Types.ObjectId,
        ref:"User"
    },
    //book id
    book_id:{
        type:mongoose.Schema.Types.ObjectId,
        ref:"Book"
    },
    //第几章节
    article_number:{        
        min:0,    //数字最小值
        max:9999,
        type:Number,
        require:[true,"第几章节"],
    },
    //章节内容
    article_doc:{
        type: String,
        maxlength: 100000,   //字符串长度
        minlength: 2,
        required: [true, "章节内容"],
        trim:true,    //自动去两侧的空格，lowercase,uppercase
    },
    //章节名字
    article_name: {
        type: String,
        maxlength: 20,   //字符串长度 
        minlength: 2,
        required: [true, "章节名字"],
        unique:true,   //表示这个属性不允许重复
        trim:true,    //自动去两侧的空格，lowercase,uppercase
    },
 
    //默认创建时间
    registerDate: { 
        type: Date,
        default: Date.now //默认值
    }
})

// 创建集合,首字母必须是 大写的 User
const Article = mongoose.model('Article', articleSchema);

//新增一个
async function add1() { 
    var data1 = await new Article({
        author_id:"5f6746e559d30800f0048c59",
        book_id:"5f675a214c484f21a45c729d",
        article_name: "第4章标题", 
        article_number: 2,
        article_doc:"第2章内容"
    }).save()
    console.log(data1)
}

//查找
async function find() {
    let data=await Article.find({})
         console.log(data)
 }
 
// find()
// add1()

module.exports=Article